<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-8-3
 * Time: 下午2:33
 */
class PosterAction extends PublicAction
{

    function _initialize()
    {
        parent::_initialize();
        if (F('Wapconfig_'.LANG_NAME)) {
            $this->wap_conf =  F('Wapconfig_'.LANG_NAME);
        } else {
            R('Admin/Wap/updateWapCache');
            $this->wap_conf =  F('Wapconfig_'.LANG_NAME);
        }
        $this->assign($this->wap_conf);
    }

    public function index()
    {
        $list = M('Poster')->select();
        $this->assign('list', $list);
        $this->assign($this->sysConfig);

        $this->display();
    }

    public function add()
    {

        if(IS_POST){
            $model = D('Poster');

            $_POST['createtime'] = time();
            $_POST['updatetime'] = time();
            if (false === $model->create()) {
                $this->error($model->getError());
            }

            if (false !== $model->add()) {
                $this->success('添加成功！',U('Poster/index'));
                exit;
            }
        }
        $this->display();
    }

    public function edit()
    {
        if(IS_POST){

            $model = D('Poster');
            $_POST['updatetime'] = time();
            $arrdata = array();

            foreach ($_POST['content'] as $k=>$res) {
                if(!empty($_POST['content'][$k])) {
                    $arrdata[$k]['filepath'] = $_POST['content'][$k];
                    $arrdata[$k]['filename'] = $_POST['content_name'][$k];
                }
            }

            $_POST['content'] = json_encode($arrdata);

            if (false === $model->create()) {
                $this->error($model->getError());
            }

            if (false !== $model->save()) {
                $this->success('修改成功！',U('Poster/index'));
                exit;
            }
        }
        $id = I('get.id');
        $data = M('Poster')->find($id);
        $file_list = json_decode($data['content'], true);
//        dump($data);exit;
        $this->assign('data', $data);
        $this->assign('file_list', $file_list);
        $this->display();
    }

    function html()
    {
        $id = I('get.id');

        $model = M('Poster');

        $list = $model->find($id);
        $list['content'] = json_decode($list['content'], true);
        $this->assign('list', $list);

        $this->assign($this->Config);

        $this->buildHtml($id,'./poster/','./poster/i.php');

        $this->success('生成成功！');
    }
}